#include "StdAfx.h"
#include "GraphicControlMedia.h"
#include "MediaObject.h"
#include "MultiMediaDlg.h"
#include "ListFileMultiMedia.h"

#include <string>
#define MY_TIMEREVENT 9991

IMPLEMENT_DYNAMIC(GraphicControlMedia,CWnd)

GraphicControlMedia::GraphicControlMedia(void)
{

}

GraphicControlMedia::~GraphicControlMedia(void)
{

}
BEGIN_MESSAGE_MAP(GraphicControlMedia, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BTN_FULL_SCREEN,OnBnClickedFullScreen)
	ON_BN_CLICKED(IDC_BTN_PLAY_PAUSE,OnBnClickedPlayPause)
	ON_BN_CLICKED(IDC_BTN_STOP,OnBnClickedStop)
	ON_BN_CLICKED(IDC_BTN_NEXT,OnBnClickedNext)
	ON_BN_CLICKED(IDC_BTN_PREVIOUS,OnBnClickedPrevious)
	ON_BN_CLICKED(IDC_CKB_MUTE,OnBnClickedMute)
	ON_BN_CLICKED(IDC_BTN_CHANGE,OnBnClickedChange)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLD_SEEK_BAR, OnNMReleasedcaptureSldSeekBar)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLD_VOLUME, OnNMReleasedcaptureSldVolume)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

GraphicControlMedia *gInstance;
GraphicControlMedia* GraphicControlMedia::ShareInstance()
{
	return gInstance;
}
int GraphicControlMedia::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	gInstance = this;
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO:  Add your specialized creation code here

	m_SliderSeeking.Create(WS_CHILD | WS_VISIBLE | TBS_HORZ, CRect(0, 0, 900, 30), this, IDC_SLD_SEEK_BAR);
	m_SliderSeeking.ShowWindow(SW_SHOW);

	m_stcTimePlaying.Create(_T("00:00:00"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 80, 30), this, NULL);
	m_stcTimePlaying.ShowWindow(SW_SHOW);

	m_stcTab.Create(_T("/"),WS_CHILD | WS_VISIBLE, CRect(0, 0, 80, 30), this,NULL);
	m_stcTab.ShowWindow(SW_SHOW);

	m_stcTimeTotal.Create(_T("00:00:00"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 80, 30), this,NULL);
	m_stcTimeTotal.ShowWindow(SW_SHOW);

	/*m_btncheckLoop.Create(_T("Loop"),WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,CRect(0,0,60,30),this,NULL);
	m_btncheckLoop.ShowWindow(SW_SHOW);*/

	m_btnPlayPause.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_PLAY_PAUSE);
	m_btnPlayPause.ShowWindow(SW_SHOW);
	m_btnPlayPause.LoadStdImage(IDB_PLAY, IDB_PLAY, IDB_PLAY,_T("PNG"));
	m_btnPlayPause.LoadAltImage(IDB_PAUSE, _T("PNG"));
	m_btnPlayPause.EnableToggle(TRUE);

	m_btnStop.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_STOP);
	m_btnStop.ShowWindow(SW_SHOW);
	m_btnStop.LoadStdImage(IDB_STOP, IDB_STOP, IDB_STOP, _T("PNG"));

	m_btnNext.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_NEXT);
	m_btnNext.ShowWindow(SW_SHOW);
	m_btnNext.LoadStdImage(IDB_NEXT, IDB_NEXT, IDB_NEXT, _T("PNG"));

	m_btnPrevious.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_PREVIOUS);
	m_btnPrevious.ShowWindow(SW_SHOW);
	m_btnPrevious.LoadStdImage(IDB_PREVIOUS, IDB_PREVIOUS, IDB_PREVIOUS, _T("PNG"));

	m_btnFullScreen.Create(_T(""),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_FULL_SCREEN);
	m_btnFullScreen.ShowWindow(SW_SHOW);
	m_btnFullScreen.LoadStdImage(IDB_FULL_SCREEN, IDB_FULL_SCREEN, IDB_FULL_SCREEN, _T("PNG"));
	m_btnFullScreen.SetToolTipText(_T("Full Screen"));

	m_btncheckmute.Create(_T(""),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,CRect(0,0,60,30),this,IDC_CKB_MUTE);
	m_btncheckmute.ShowWindow(SW_SHOW);
	m_btncheckmute.LoadStdImage(IDB_VOLUME, IDB_VOLUME, IDB_VOLUME, _T("PNG"));
	m_btncheckmute.LoadAltImage(IDB_VOLUME_MUTE, _T("PNG"));
	m_btncheckmute.EnableToggle(TRUE);

	m_btnChangeScreen.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_CHANGE);
	m_btnChangeScreen.ShowWindow(SW_SHOW);
	m_btnChangeScreen.LoadStdImage(IDB_CHANGE, IDB_CHANGE, IDB_CHANGE, _T("PNG"));

	m_SliderVolume.Create(WS_CHILD|WS_VISIBLE|TBS_HORZ,CRect(0,0,0,30),this,IDC_SLD_VOLUME);
    m_SliderVolume.ShowWindow(SW_SHOW);
	
	m_SliderSeeking.SetRange(MIN_RANGE_SEEK_SLIDER, MAX_RANGE_SEEK_SLIDER);
	m_SliderVolume.SetRange(MIN_RANGE_VOL_SLIDER, MAX_RANGE_VOL_SLIDER);
	
	CMultiMediaDlg::ShareInstance()->m_nVol = -1;
	m_SliderVolume.SetPos(CMultiMediaDlg::ShareInstance()->m_nVol);
	
	m_bCheck = false;
	m_CurVolume = 0;
	return 0;
}

void GraphicControlMedia::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
    CWnd *parent = GetParent();
	CRect rect;
	this->GetClientRect(&rect);
	parent->GetClientRect(&rect);
	CRect rtViewArea(0,rect.bottom-rect.top - 90,rect.right, rect.bottom);//here
   

	int nMarginLeft = (rtViewArea.right - rtViewArea.left )/3;
	int nMarginTop  = (rtViewArea.bottom - rtViewArea.top );
	int width = rtViewArea.right - rtViewArea.left;

	m_SliderSeeking.SetWindowPos(parent,	0,					  nMarginTop - 90, rtViewArea.right,	30, SWP_NOZORDER);
	m_stcTimePlaying.SetWindowPos(parent,	nMarginLeft / 8,	  nMarginTop / 2, 70,				30, SWP_NOZORDER);
	m_stcTab.SetWindowPos(parent,			nMarginLeft / 8 + 70, nMarginTop / 2, 5,				30, SWP_NOZORDER);
	m_stcTimeTotal.SetWindowPos(parent,		nMarginLeft / 8 + 80, nMarginTop / 2, 70,				30, SWP_NOZORDER);
	m_btnPlayPause.SetWindowPos(parent,     width / 2,            nMarginTop / 2 - 10,	  48,				48, SWP_NOZORDER);
	m_btnNext.SetWindowPos(parent,          width / 2 + 48,       nMarginTop / 2 - 5, 40,				40, SWP_NOZORDER);
	m_btnPrevious.SetWindowPos(parent,      width / 2 - 40,       nMarginTop / 2 - 5, 40,				40, SWP_NOZORDER);
	m_btnStop.SetWindowPos(parent,          width / 2 + 100,      nMarginTop / 2 - 5, 40,				40, SWP_NOZORDER);
	m_btnChangeScreen.SetWindowPos(parent,  width - 330,		  nMarginTop / 2 - 5,				40,		 40, SWP_NOZORDER);
	m_btnFullScreen.SetWindowPos(parent,    width - 285,		  nMarginTop / 2 - 5,	45,				45, SWP_NOZORDER);
	m_btncheckmute.SetWindowPos(parent,     width - 240,		  nMarginTop / 2 - 5, 40,				40, SWP_NOZORDER);
	m_SliderVolume.SetWindowPos(parent,     width - 200,		  nMarginTop / 2,   200,				30, SWP_NOZORDER);
}

void GraphicControlMedia::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages
}

BOOL GraphicControlMedia::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	//CRect rect;

	//GetClientRect(&rect);
	//CBrush myBrush(RGB(0, 255, 0));    // dialog background color
	//CBrush *pOld = pDC->SelectObject(&myBrush);
	//BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
	//pDC->SelectObject(pOld);    // restore old brush
	//return bRes;   

	return CWnd::OnEraseBkgnd(pDC);
}
void GraphicControlMedia::OnBnClickedFullScreen()
{
	LONGLONG cur = 0;
	KillTimer(MY_TIMEREVENT);
	//SetTimer(MY_TIMEREVENT,1000,(TIMERPROC)NULL);// set timer 1s
	CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaSeeking->GetCurrentPosition(&cur);
	m_fullScreen.m_currentPos = cur;
	if(CMultiMediaDlg::ShareInstance()->m_changleFullScreen)
	{

	m_fullScreen.m_path =  CMultiMediaDlg::ShareInstance()->m_strFilePath2; 

	}
	else
	{
		
			m_fullScreen.m_path =  CMultiMediaDlg::ShareInstance()->arrMediaObjects[ListFileMultiMedia::ShareInstance()->m_nNumOfPlaylist].GetFilePath();


	}
	m_fullScreen.m_lVolume =(long)m_SliderVolume.GetPos()*1000;
	CMultiMediaDlg::ShareInstance()->m_VideoMng.SetVolume(-10000);
	GraphicShowVideo::ShareInstance()->m_Sub->ShowWindow(FALSE);
	
	GraphicShowVideo::ShareInstance()->m_showSmall.ShowWindow(SW_HIDE);
	ShowWindow(FALSE);
	

	int tt = m_fullScreen.DoModal();
	
	if (tt == IDD_FULL_VIDEO_SCREEN){
		
		CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Clean();
		CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Stop();
		
		
	}
	else
	{
		cur = m_fullScreen.m_currentPos;
		CMultiMediaDlg::ShareInstance()->m_nVol=m_fullScreen.m_lVolume;
		CMultiMediaDlg::ShareInstance()->MediaVolume(CMultiMediaDlg::ShareInstance()->m_nVol);
		CMultiMediaDlg::ShareInstance()->m_VideoMng.SetVolume(-1000);
		SetTimer(MY_TIMEREVENT,1000,(TIMERPROC)NULL);// set timer 1s
		CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaControl->Run();
		LONGLONG lTimeLineUnit =0;
		lTimeLineUnit=CMultiMediaDlg::ShareInstance()->m_VideoMng.m_length/MAX_RANGE_SEEK_SLIDER; //slide range[0-100]
		int nPos = (int)cur/lTimeLineUnit;
		m_SliderSeeking.SetPos(nPos);
		CMultiMediaDlg::ShareInstance()->m_VideoMng.SetPosition2(cur);
		GraphicShowVideo::ShareInstance()->m_Sub->ShowWindow(TRUE);
		if(CMultiMediaDlg::ShareInstance()->m_bCheckFull==true)
		{

			GraphicShowVideo::ShareInstance()->m_showSmall.ShowWindow(SW_SHOW);

		}
		
	}
	
    this->ShowWindow(TRUE);
}

void GraphicControlMedia::OnBnClickedPlayPause()
{
	if(CMultiMediaDlg::ShareInstance()->m_bIsStop)
	{
	   CMultiMediaDlg::ShareInstance()->MediaPlay();
	   if(CMultiMediaDlg::ShareInstance()->m_Audio != true&& CMultiMediaDlg::ShareInstance()->m_bToggle == true)
	   {
			m_btnChangeScreen.EnableButton(true);
			m_btnChangeScreen.EnableWindow(true);
			CMultiMediaDlg::ShareInstance()->m_VideoMng2.SetFilePath( CMultiMediaDlg::ShareInstance()->m_strFilePath2);
			CMultiMediaDlg::ShareInstance()->m_gShowVideo.m_showSmall.SetWndParentForVideoPlayerManager2();
			CMultiMediaDlg::ShareInstance()->m_VideoMng2.SetRectPlayer( CMultiMediaDlg::ShareInstance()->m_gShowVideo.m_showSmall.m_rtPlayer2);
			CMultiMediaDlg::ShareInstance()->m_VideoMng2.MultiMedia_Play();
			CMultiMediaDlg::ShareInstance()->m_VideoMng2.SetVolume(-10000);
			CMultiMediaDlg::ShareInstance()->UpdateVideoScreen2();
	   }

		CMultiMediaDlg::ShareInstance()->m_bIsStop = false;
	}
	else
	{
		if(CMultiMediaDlg::ShareInstance()->m_bIsPlaying)
		{
			CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Pause();
			if(CMultiMediaDlg::ShareInstance()->m_Audio !=true && CMultiMediaDlg::ShareInstance()->m_bToggle == true)
			{
            CMultiMediaDlg::ShareInstance()->m_VideoMng2.MultiMedia_Pause();
			}
			CMultiMediaDlg::ShareInstance()->m_bIsPlaying = false;
		}
		else
		{
			CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Resume();
			if(CMultiMediaDlg::ShareInstance()->m_Audio !=true&& CMultiMediaDlg::ShareInstance()->m_bToggle == true)
			{
			CMultiMediaDlg::ShareInstance()->m_VideoMng2.MultiMedia_Resume();
			}
			CMultiMediaDlg::ShareInstance()->m_bIsPlaying = true;
		}
	}
}

void GraphicControlMedia::OnBnClickedStop()
{
	CMultiMediaDlg::ShareInstance()->MediaStop();
	if(CMultiMediaDlg::ShareInstance()->m_Audio !=true&& CMultiMediaDlg::ShareInstance()->m_bToggle == true)
	{
		CMultiMediaDlg::ShareInstance()->m_VideoMng2.MultiMedia_Stop();
		
	}
}

void GraphicControlMedia::OnBnClickedNext()
{
	CMultiMediaDlg::ShareInstance()->NextOrPrevPlayList(true);
	ListFileMultiMedia::ShareInstance()->PlayCurrentTrack();
}

void GraphicControlMedia::OnBnClickedPrevious()
{
	CMultiMediaDlg::ShareInstance()->NextOrPrevPlayList(false);
	ListFileMultiMedia::ShareInstance()->PlayCurrentTrack();

}

void GraphicControlMedia::OnNMReleasedcaptureSldSeekBar(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
	CString strTime = CMultiMediaDlg::ShareInstance()->m_VideoMng.SetPosition(m_SliderSeeking.GetPos());
	m_stcTimePlaying.SetWindowTextW((LPCTSTR)strTime);
	if(m_SliderSeeking.GetPos() == MAX_RANGE_SEEK_SLIDER)
	{
	}
	else
	{
	}
}

void GraphicControlMedia::OnNMReleasedcaptureSldVolume(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
	CMultiMediaDlg::ShareInstance()->m_nVol = m_SliderVolume.GetPos();
	CMultiMediaDlg::ShareInstance()->MediaVolume(CMultiMediaDlg::ShareInstance()->m_nVol);
	m_btncheckmute.SetImage(0);
}

void GraphicControlMedia::OnBnClickedMute()
{   
	int curVol = 0;
	
	
	if(m_bCheck == true)
	{   
		m_CurVolume = m_SliderVolume.GetPos();
		CMultiMediaDlg::ShareInstance()->m_nVol = MIN_RANGE_VOL_SLIDER;
		m_SliderVolume.SetPos(-10);
		m_bCheck = false;
	}
	else
	{
		CMultiMediaDlg::ShareInstance()->m_nVol = m_CurVolume;
        m_SliderVolume.SetPos(m_CurVolume);
		m_bCheck = true;
	
	}
	CMultiMediaDlg::ShareInstance()->MediaVolume(CMultiMediaDlg::ShareInstance()->m_nVol);
   

}

void GraphicControlMedia::SetEnableOrDisableControl(bool bFlag)
{
	m_btnPlayPause.EnableButton(bFlag);
	m_btnStop.EnableButton(bFlag);
	m_btnNext.EnableButton(bFlag);
	m_btnPrevious.EnableButton(bFlag);
	m_btncheckmute.EnableButton(bFlag);
	m_btnChangeScreen.EnableButton(bFlag);
	m_SliderSeeking.EnableWindow(bFlag);
	m_SliderVolume.EnableWindow(bFlag);
	m_btnPlayPause.EnableWindow(bFlag);
	m_btnStop.EnableWindow(bFlag);
	m_btnNext.EnableWindow(bFlag);
	m_btnPrevious.EnableWindow(bFlag);
	/*if(CMultiMediaDlg::ShareInstance()->m_Audio == true)
	{
		 m_btnFullScreen.EnableWindow(false);
		 m_btnFullScreen.EnableButton(false);
	}
	else
	{ */  
	     /*m_btnFullScreen.EnableButton(bFlag);
		 m_btnFullScreen.EnableWindow(bFlag);*/
		
	//}
	
	m_btncheckmute.EnableWindow(bFlag);
	m_btnChangeScreen.EnableWindow(bFlag);

	CMenu *menuCtrl = CMultiMediaDlg::ShareInstance()->GetMenu();
	menuCtrl->GetSubMenu(1);
	if(bFlag)
		{
		menuCtrl->EnableMenuItem(ID_CONTROL_PLAY,MF_ENABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_STOP,MF_ENABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_NEXT,MF_ENABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_PREVIOUS,MF_ENABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_MUTE,MF_ENABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_FULLSCREEN, MF_ENABLED);
	}
	else
	{
		menuCtrl->EnableMenuItem(ID_CONTROL_PLAY,MF_DISABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_STOP,MF_DISABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_NEXT,MF_DISABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_PREVIOUS,MF_DISABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_MUTE,MF_DISABLED);
		menuCtrl->EnableMenuItem(ID_CONTROL_FULLSCREEN, MF_DISABLED);
	}
}

void GraphicControlMedia::OnBnClickedChange()
{
	CMultiMediaDlg::ShareInstance()->OnChangevideo();
}